import { Application } from 'egg';
import { DataTypes, Model } from 'sequelize';

export interface  ExclusiveRestaurantAttributes {
  id?: number;
  name: string;
  address: string;
  phone: string;
  open_time: string;
  description: string;
  pictures: string;
  tags: string;
  grade: number;
  status: 'pending' | 'approved' | 'rejected';
  user_id: number;
  created_at?: Date;
  updated_at?: Date;
}

export interface ExclusiveRestaurantInstance extends Model<ExclusiveRestaurantAttributes>, ExclusiveRestaurantAttributes {}

export default (app: Application) => {
  const { STRING, INTEGER, TEXT, DECIMAL, ENUM, DATE } = DataTypes;

  const ExclusiveRestaurant = app.model.define<ExclusiveRestaurantInstance>('ExclusiveRestaurant', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    name: {
      type: STRING(100),
      allowNull: false,
      comment: '店名',
    },
    address: {
      type: STRING(255),
      allowNull: true,
      comment: '店铺地址',
    },
    phone: {
      type: STRING(20),
      allowNull: true,
      comment: '电话',
    },
    open_time: {
      type: STRING(100),
      allowNull: true,
      comment: '营业时间',
    },
    description: {
      type: TEXT,
      allowNull: true,
      comment: '店铺介绍',
    },
    pictures: {
      type: STRING(500),
      allowNull: true,
      comment: '图片链接',
    },
    tags: {
      type: STRING(255),
      allowNull: true,
      comment: '标签ID列表，逗号分隔',
    },
    grade: {
      type: DECIMAL(3, 2),
      defaultValue: 0,
      comment: '评分',
    },
    status: {
      type: ENUM('pending', 'approved', 'rejected'),
      defaultValue: 'pending',
      comment: '审核状态',
    },
    user_id: {
      type: INTEGER,
      allowNull: false,
      comment: '申请用户ID',
    },
    created_at: {
      type: DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    updated_at: {
      type: DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
  }, {
    tableName: 'exclusive_restaurants',
    comment: '专属餐厅表',
  });

  // 关联关系
  (ExclusiveRestaurant as any).associate = () => {
    (ExclusiveRestaurant as any).belongsTo(app.model.User, { foreignKey: 'user_id', as: 'user' });
  };

  return ExclusiveRestaurant;
};